#!/bin/bash

cd PathTo/Datafiles/$1

for f in *.MP4; do
   # Get filename without path and extension:
   base=${f%.*}
   echo "$base"

   if [ ! -f $base\_audio.mp3 ]; then
      echo "Extract audio from MP4 file."
      ffmpeg -i $f $base\_audio.mp3
   else
      echo "$base\_audio.mp3 exists."
   fi

   cd ../../../

   if [ ! -f $base\_subtracted_background.avi ]; then
      echo "Create background-subtracted video as AVI file."
      /usr/bin/env python3 videoanalysis_new.py $1 $base
   else
      echo "$base\_subtracted_background.avi exists."
   fi

   cd PathTo/Datafiles/$1

   if [ ! -f $base.avi ]; then
      echo "Combine audio and video to new AVI file."
      ffmpeg -y -i $base\_audio.mp3 -i $base\_subtracted_background.avi -c:v copy -c:a aac -strict  experimental -map 0:0 -map 1:0 $base.avi
   else
      echo "$base.avi exists."
   fi
done

